<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机"></el-input>
      </el-form-item>
      <el-form-item label="登陆密码" prop="password">
        <el-input  type="password" v-model="dataForm.password" placeholder="登陆密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="secPassword">
        <el-input  type="secPassword" v-model="dataForm.secPassword" placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="用户姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="用户姓名"></el-input>
      </el-form-item>
      <!-- <el-form-item label="代理用户" prop="parentUserId">
        <el-select v-model="dataForm.parentUserId" placeholder="请选择代理用户">
          <el-option
            v-for="miniUser in miniUserOptions"
            :key="miniUser.id"
            :label="miniUser.name"
            :value="miniUser.id">
          </el-option>
        </el-select>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  import { isMobile } from '@/utils/validate'
  import { isPassword } from '@/utils/validate'
  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
            callback(new Error('手机号格式不对'))
        } else {
            callback()
        }
      }
       var validateComfirmPassword = (rule, value, callback) => {
        if (!isPassword(value)) {
          callback(new Error('6-12位的字母、数字'))
        }else if (this.dataForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      var validateNewPassword = (rule, value, callback) => {
        if (!isPassword(value)) {
          callback(new Error('6-12位的字母、数字'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        userType:'AGENT',
        miniUserOptions: [],
        dataForm: {
          id: '',
          email: '',
          mobile: '',
          password: '',
          name: '',
          username: '',
          parentUserId: '',
          secPassword: ''
        },
        dataRule: {
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' },
            { validator: validateNewPassword, trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          secPassword: [
            { required: true, message: '新密码不能为空', trigger: 'blur' }
            , { validator: validateComfirmPassword, trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id
        this.miniUserOptions = []
        this.visible = true

        var params = {
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            userType_eq: this.userType
        }
        API.agencyminiuser.list(params).then(({data}) => {
          if (data && data.code === 0) {
            this.miniUserOptions = data.page.content
          } else {
            this.miniUserOptions = []
          }
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            //修改
            console.log(this.dataForm.id)
            API.agencyminiuser.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                console.log(data)
                this.dataForm.email = data.user.email
                this.dataForm.mobile = data.user.mobile
                this.dataForm.password = data.user.password
                this.dataForm.name = data.user.name
                this.dataForm.username = data.user.username
                this.dataForm.parentUserId = data.user.parentUserId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'email': this.dataForm.email || undefined,
              'mobile': this.dataForm.mobile || undefined,
              'password': this.dataForm.password || undefined,
              'name': this.dataForm.name || undefined,
              'username': this.dataForm.username || undefined,
              'parentUserId': this.dataForm.parentUserId || undefined
            }
            var tick = !this.dataForm.id ? API.agencyminiuser.add(params) : API.agencyminiuser.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
